#!/bin/bash

if [ ! -f VERSION ]; then
  echo "ERROR: must be executed from top directory"
  exit 1
fi

version=`cat VERSION`
pdf_build_dir=build.pdf
PUBLIC="plweb@oehoe:srv/plweb/data/download/devel"
export MACOSX_DEPLOYMENT_TARGET=10.15

build_pdf()
{ rm -rf build.pdf
  mkdir build.pdf
  ( cd build.pdf
    cmake -DBUILD_PDF_DOCUMENTATION=ON -G Ninja ..
    ninja
  )
}

# Build the Windows version using the Docker from
# https://github.com/SWI-Prolog/docker-swipl-build-mingw.git

MINGW_DOCKER_DIR=$HOME/src/docker/docker-swipl-build-mingw

build_win64()
{ make -C $MINGW_DOCKER_DIR SWIPLSRC=`pwd` win64
}

# Used on the M1 where we build using Clang and Qt
build_macosx()
{ rm -rf build.macosx
  mkdir build.macosx
  ( cd build.macosx
    export PATH=$HOME/deps/bin:$QT_BIN:$PATH
    MACOSX_DEPLOYMENT_TARGET=10.14 \
	cmake -DCMAKE_BUILD_TYPE=Release \
	      -DSWIPL_CC=cc -DSWIPL_CXX=c++ \
	      -DUSE_GMP=OFF \
	      -DMACOSX_DEPENDENCIES_FROM=$HOME/deps \
              -DBUILD_MACOS_BUNDLE=ON \
	      -DJAVA_COMPATIBILITY=ON \
	      -G Ninja ..
    ninja
    cpack
  )
}


build_macos_universal_binary()
{ rm -rf build.macosx-fat
  mkdir build.macosx-fat
  ( cd build.macosx-fat
    PREFIX=$HOME/deps
    export PKG_CONFIG_LIBDIR="/usr/lib/pkgconfig:$PREFIX/lib/pkgconfig"
    export CMAKE_PREFIX_PATH="$PREFIX:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr"
    CUFLAGS="-arch x86_64"
    export CFLAGS="-Wno-nullability-completeness $CUFLAGS"
    export CXXFLAGS="$CFLAGS"
    export PATH=$HOME/deps/bin:$QT_BIN:$PATH
    MACOSX_DEPLOYMENT_TARGET=$MACOSX_DEPLOYMENT_TARGET \
	cmake -DCMAKE_BUILD_TYPE=Release \
	      -DSWIPL_CC=cc -DSWIPL_CXX=c++ \
	      -DMACOSX_DEPENDENCIES_FROM=$HOME/deps \
	      -DPKG_CONFIG_EXECUTABLE=/opt/local/bin/pkg-config \
              -DMACOS_UNIVERSAL_BINARY=ON \
              -DBUILD_MACOS_BUNDLE=ON \
	      -DJAVA_COMPATIBILITY=ON \
	      -DCMAKE_FRAMEWORK_PATH=/Library/Frameworks \
	      -G Ninja ..
    ninja
    cpack
  )
}


# Use gcc 13 from Macport `port install gcc13`
build_macosx_gcc()
{ rm -rf build.macosx-gcc
  mkdir build.macosx-gcc
  ( cd build.macosx-gcc
    export PATH=$HOME/deps/bin:$QT_BIN:$PATH
    export CC=gcc-mp-13
    MACOSX_DEPLOYMENT_TARGET=$MACOSX_DEPLOYMENT_TARGET \
        cmake -DCMAKE_BUILD_TYPE=PGO \
	      -DSWIPL_CC=cc -DSWIPL_CXX=c++ \
              -DMACOSX_DEPENDENCIES_FROM=$HOME/deps \
              -DBUILD_MACOS_BUNDLE=ON \
              -DJAVA_COMPATIBILITY=ON \
              -G Ninja ..
    ninja
    cpack
  )
}

build_source()
{ ./scripts/make-src-tape
}

ubuntu()
{ $HOME/src/docker/ubuntu/run.sh -C $(pwd) $*
}

# Build a single PPA in case something went wrong and we need
# to retry.
build_PPA
{ distro=$1

  git checkout ppa
  git rebase master
  ubuntu ./scripts/make-ppa --distro=$distro --push
  git checkout master
  git submodule update debian
}

# Build all PPAs
build_PPAs()
{ git branch -D ppa || true
  git checkout -b ppa
  for distro in $(./scripts/make-ppa --list-distros); do
    ubuntu ./scripts/make-ppa --distro=$distro --push
  done
  git checkout master
  git submodule update debian
}

is_clean_release()
{ if [ V${version} != $(git describe) ]; then
    echo "ERROR: Git tag is inconsistent with release"
    return 1
  fi
  if [ ! -z "$(git diff -q)" ]; then
    echo "ERROR: Working directory is dirty"
    return 1
  fi
}

list_file()
{ if [ -f "$1" ]; then
     ls -l "$1"
  fi
}

list_built()
{ if [ $(uname) = Darwin ]; then
    list_file build.macosx-fat/swipl-${version}-1.fat.dmg
    list_file build.macosx-gcc/swipl-${version}-1.x86_64.dmg
    list_file build.macosx/swipl-${version}-1.x86_64.dmg
  else
    ls -l build.win64/swipl-${version}-1.x64.exe
    ls -l build.pdf/man/SWI-Prolog-$version.pdf
    ls -l ../swipl-$version.tar.gz
  fi
}

build()
{ is_clean_release && force_build
}

force_build()
{ if [ $(uname) = Darwin ]; then
    if uname -a | grep arm64 > /dev/null; then
      build_macos_universal_binary
    else
      build_macosx_gcc
    fi
  else
    build_pdf
    build_win64
    build_source
    [ "$PPA" = no ] || build_PPAs
  fi

  list_built
}

################
# Uploading

upload_file()
{ if [ -f "$2" ]; then
    rsync -Pvu "$2" ${PUBLIC}/$1
  fi
}

upload_win64()
{ upload_file bin build.win64/swipl-${version}-1.x64.exe
}

upload_macosx()
{ if [ -f build.macosx-fat/swipl-${version}-1.fat.dmg ]; then
    echo "Uploading universal binary version"
    upload_file bin build.macosx-fat/swipl-${version}-1.fat.dmg
  elif [ -f build.macosx-gcc/swipl-${version}-1.x86_64.dmg ]; then
    echo "Uploading fast GCC version"
    upload_file bin build.macosx-gcc/swipl-${version}-1.x86_64.dmg
  else
    echo "WARNING: uploading slow Clang version"
    upload_file bin build.macosx/swipl-${version}-1.x86_64.dmg
  fi
}

upload_pdf()
{ upload_file doc build.pdf/man/SWI-Prolog-$version.pdf
}

upload_src()
{ upload_file src ../swipl-$version.tar.gz
}

upload()
{ if [ $(uname) = Darwin ]; then
    upload_macosx
  else
    upload_win64
    upload_pdf
    upload_src
  fi
}
